﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Text;
using System.Data.SqlClient;

namespace 数据函数库
{
    [Serializable]
    public class 传感器阈值Data
    {

        public const string IPField = "@IP";

        public const string GREENHOUSEADDRField = "@GreenhouseAddr";

        public const string NOTENUMField = "@NoteNum";

        public const string TIMEField = "@Time";

        public const string 空气温度上限Field = "@空气温度上限";

        public const string 空气温度下限Field = "@空气温度下限";

        public const string 空气湿度上限Field = "@空气湿度上限";

        public const string 空气湿度下限Field = "@空气湿度下限";

        public const string 光照上限Field = "@光照上限";

        public const string 光照下限Field = "@光照下限";

        public const string 土壤温度上限Field = "@土壤温度上限";

        public const string 土壤温度下限Field = "@土壤温度下限";

        public const string 土壤湿度上限Field = "@土壤湿度上限";

        public const string 土壤湿度下限Field = "@土壤湿度下限";

        public const string 二氧化碳上限Field = "@二氧化碳上限";

        public const string 二氧化碳下限Field = "@二氧化碳下限";

        public const string 土壤盐分上限Field = "@土壤盐分上限";

        public const string 土壤盐分下限Field = "@土壤盐分下限";

        private String _IP;

        private Int32 _GreenhouseAddr;

        private Int32 _NoteNum;

        private DateTime _Time;

        private Int32 _空气温度上限;

        private Int32 _空气温度下限;

        private Int32 _空气湿度上限;

        private Int32 _空气湿度下限;

        private Int32 _光照上限;

        private Int32 _光照下限;

        private Int32 _土壤温度上限;

        private Int32 _土壤温度下限;

        private Int32 _土壤湿度上限;

        private Int32 _土壤湿度下限;

        private Int32 _二氧化碳上限;

        private Int32 _二氧化碳下限;

        private Int32 _土壤盐分上限;

        private Int32 _土壤盐分下限;

        public String IP
        {
            get { return _IP; }
            set { _IP = value; }
        }
        public Int32 GreenhouseAddr
        {
            get { return _GreenhouseAddr; }
            set { _GreenhouseAddr = value; }
        }
        public Int32 NoteNum
        {
            get { return _NoteNum; }
            set { _NoteNum = value; }
        }
        public DateTime Time
        {
            get { return _Time; }
            set { _Time = value; }
        }
        public Int32 空气温度上限
        {
            get { return _空气温度上限; }
            set { _空气温度上限 = value; }
        }
        public Int32 空气温度下限
        {
            get { return _空气温度下限; }
            set { _空气温度下限 = value; }
        }
        public Int32 空气湿度上限
        {
            get { return _空气湿度上限; }
            set { _空气湿度上限 = value; }
        }
        public Int32 空气湿度下限
        {
            get { return _空气湿度下限; }
            set { _空气湿度下限 = value; }
        }
        public Int32 光照上限
        {
            get { return _光照上限; }
            set { _光照上限 = value; }
        }
        public Int32 光照下限
        {
            get { return _光照下限; }
            set { _光照下限 = value; }
        }
        public Int32 土壤温度上限
        {
            get { return _土壤温度上限; }
            set { _土壤温度上限 = value; }
        }
        public Int32 土壤温度下限
        {
            get { return _土壤温度下限; }
            set { _土壤温度下限 = value; }
        }
        public Int32 土壤湿度上限
        {
            get { return _土壤湿度上限; }
            set { _土壤湿度上限 = value; }
        }
        public Int32 土壤湿度下限
        {
            get { return _土壤湿度下限; }
            set { _土壤湿度下限 = value; }
        }
        public Int32 二氧化碳上限
        {
            get { return _二氧化碳上限; }
            set { _二氧化碳上限 = value; }
        }
        public Int32 二氧化碳下限
        {
            get { return _二氧化碳下限; }
            set { _二氧化碳下限 = value; }
        }
        public Int32 土壤盐分上限
        {
            get { return _土壤盐分上限; }
            set { _土壤盐分上限 = value; }
        }
        public Int32 土壤盐分下限
        {
            get { return _土壤盐分下限; }
            set { _土壤盐分下限 = value; }
        }
        public 传感器阈值Data()
        {
            _IP = "";
            _GreenhouseAddr = 0;
            _NoteNum = 0;
            _Time = DateTime.Now;
            _空气温度上限 = 0;
            _空气温度下限 = 0;
            _空气湿度上限 = 0;
            _空气湿度下限 = 0;
            _光照上限 = 0;
            _光照下限 = 0;
            _土壤温度上限 = 0;
            _土壤温度下限 = 0;
            _土壤湿度上限 = 0;
            _土壤湿度下限 = 0;
            _二氧化碳上限 = 0;
            _二氧化碳下限 = 0;
            _土壤盐分上限 = 0;
            _土壤盐分下限 = 0;
        }
    }

    public class 传感器阈值DataFactory
    {
        public static 传感器阈值Data Construct(IDataReader reader)
        {
            传感器阈值Data data = new 传感器阈值Data();

            try { data.IP = Convert.ToString(reader["IP"]); }
            catch { }

            try { data.GreenhouseAddr = Convert.ToInt32(reader["GreenhouseAddr"]); }
            catch { }

            try { data.NoteNum = Convert.ToInt32(reader["NoteNum"]); }
            catch { }

            try { data.Time = Convert.ToDateTime(reader["Time"]); }
            catch { }

            try { data.空气温度上限 = Convert.ToInt32(reader["空气温度上限"]); }
            catch { }

            try { data.空气温度下限 = Convert.ToInt32(reader["空气温度下限"]); }
            catch { }

            try { data.空气湿度上限 = Convert.ToInt32(reader["空气湿度上限"]); }
            catch { }

            try { data.空气湿度下限 = Convert.ToInt32(reader["空气湿度下限"]); }
            catch { }

            try { data.光照上限 = Convert.ToInt32(reader["光照上限"]); }
            catch { }

            try { data.光照下限 = Convert.ToInt32(reader["光照下限"]); }
            catch { }

            try { data.土壤温度上限 = Convert.ToInt32(reader["土壤温度上限"]); }
            catch { }

            try { data.土壤温度下限 = Convert.ToInt32(reader["土壤温度下限"]); }
            catch { }

            try { data.土壤湿度上限 = Convert.ToInt32(reader["土壤湿度上限"]); }
            catch { }

            try { data.土壤湿度下限 = Convert.ToInt32(reader["土壤湿度下限"]); }
            catch { }

            try { data.二氧化碳上限 = Convert.ToInt32(reader["二氧化碳上限"]); }
            catch { }

            try { data.二氧化碳下限 = Convert.ToInt32(reader["二氧化碳下限"]); }
            catch { }

            try { data.土壤盐分上限 = Convert.ToInt32(reader["土壤盐分上限"]); }
            catch { }

            try { data.土壤盐分下限 = Convert.ToInt32(reader["土壤盐分下限"]); }
            catch { }

            return data;
        }
        public static 传感器阈值Data Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static 传感器阈值Data Construct(DataTable dt, int rowIndex)
        {
            传感器阈值Data data = new 传感器阈值Data();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.IP = Convert.ToString(dr["IP"]); }
            catch { }

            try { data.GreenhouseAddr = Convert.ToInt32(dr["GreenhouseAddr"]); }
            catch { }

            try { data.NoteNum = Convert.ToInt32(dr["NoteNum"]); }
            catch { }

            try { data.Time = Convert.ToDateTime(dr["Time"]); }
            catch { }

            try { data.空气温度上限 = Convert.ToInt32(dr["空气温度上限"]); }
            catch { }

            try { data.空气温度下限 = Convert.ToInt32(dr["空气温度下限"]); }
            catch { }

            try { data.空气湿度上限 = Convert.ToInt32(dr["空气湿度上限"]); }
            catch { }

            try { data.空气湿度下限 = Convert.ToInt32(dr["空气湿度下限"]); }
            catch { }

            try { data.光照上限 = Convert.ToInt32(dr["光照上限"]); }
            catch { }

            try { data.光照下限 = Convert.ToInt32(dr["光照下限"]); }
            catch { }

            try { data.土壤温度上限 = Convert.ToInt32(dr["土壤温度上限"]); }
            catch { }

            try { data.土壤温度下限 = Convert.ToInt32(dr["土壤温度下限"]); }
            catch { }

            try { data.土壤湿度上限 = Convert.ToInt32(dr["土壤湿度上限"]); }
            catch { }

            try { data.土壤湿度下限 = Convert.ToInt32(dr["土壤湿度下限"]); }
            catch { }

            try { data.二氧化碳上限 = Convert.ToInt32(dr["二氧化碳上限"]); }
            catch { }

            try { data.二氧化碳下限 = Convert.ToInt32(dr["二氧化碳下限"]); }
            catch { }

            try { data.土壤盐分上限 = Convert.ToInt32(dr["土壤盐分上限"]); }
            catch { }

            try { data.土壤盐分下限 = Convert.ToInt32(dr["土壤盐分下限"]); }
            catch { }

            return data;
        }
        public static 传感器阈值Data Construct(DataGridView dgv, int rowIndex)
        {
            传感器阈值Data data = new 传感器阈值Data();

            if (dgv.Rows.Count < rowIndex + 1)

                return null;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return null;

            try { data.IP = Convert.ToString(dr["IP"]); }
            catch { }

            try { data.GreenhouseAddr = Convert.ToInt32(dr["GreenhouseAddr"]); }
            catch { }

            try { data.NoteNum = Convert.ToInt32(dr["NoteNum"]); }
            catch { }

            try { data.Time = Convert.ToDateTime(dr["Time"]); }
            catch { }

            try { data.空气温度上限 = Convert.ToInt32(dr["空气温度上限"]); }
            catch { }

            try { data.空气温度下限 = Convert.ToInt32(dr["空气温度下限"]); }
            catch { }

            try { data.空气湿度上限 = Convert.ToInt32(dr["空气湿度上限"]); }
            catch { }

            try { data.空气湿度下限 = Convert.ToInt32(dr["空气湿度下限"]); }
            catch { }

            try { data.光照上限 = Convert.ToInt32(dr["光照上限"]); }
            catch { }

            try { data.光照下限 = Convert.ToInt32(dr["光照下限"]); }
            catch { }

            try { data.土壤温度上限 = Convert.ToInt32(dr["土壤温度上限"]); }
            catch { }

            try { data.土壤温度下限 = Convert.ToInt32(dr["土壤温度下限"]); }
            catch { }

            try { data.土壤湿度上限 = Convert.ToInt32(dr["土壤湿度上限"]); }
            catch { }

            try { data.土壤湿度下限 = Convert.ToInt32(dr["土壤湿度下限"]); }
            catch { }

            try { data.二氧化碳上限 = Convert.ToInt32(dr["二氧化碳上限"]); }
            catch { }

            try { data.二氧化碳下限 = Convert.ToInt32(dr["二氧化碳下限"]); }
            catch { }

            try { data.土壤盐分上限 = Convert.ToInt32(dr["土壤盐分上限"]); }
            catch { }

            try { data.土壤盐分下限 = Convert.ToInt32(dr["土壤盐分下限"]); }
            catch { }

            return data;
        }
        public static 传感器阈值Data Construct(DataRow dr)
        {
            传感器阈值Data data = new 传感器阈值Data();

            try { data.IP = Convert.ToString(dr["IP"]); }
            catch { }

            try { data.GreenhouseAddr = Convert.ToInt32(dr["GreenhouseAddr"]); }
            catch { }

            try { data.NoteNum = Convert.ToInt32(dr["NoteNum"]); }
            catch { }

            try { data.Time = Convert.ToDateTime(dr["Time"]); }
            catch { }

            try { data.空气温度上限 = Convert.ToInt32(dr["空气温度上限"]); }
            catch { }

            try { data.空气温度下限 = Convert.ToInt32(dr["空气温度下限"]); }
            catch { }

            try { data.空气湿度上限 = Convert.ToInt32(dr["空气湿度上限"]); }
            catch { }

            try { data.空气湿度下限 = Convert.ToInt32(dr["空气湿度下限"]); }
            catch { }

            try { data.光照上限 = Convert.ToInt32(dr["光照上限"]); }
            catch { }

            try { data.光照下限 = Convert.ToInt32(dr["光照下限"]); }
            catch { }

            try { data.土壤温度上限 = Convert.ToInt32(dr["土壤温度上限"]); }
            catch { }

            try { data.土壤温度下限 = Convert.ToInt32(dr["土壤温度下限"]); }
            catch { }

            try { data.土壤湿度上限 = Convert.ToInt32(dr["土壤湿度上限"]); }
            catch { }

            try { data.土壤湿度下限 = Convert.ToInt32(dr["土壤湿度下限"]); }
            catch { }

            try { data.二氧化碳上限 = Convert.ToInt32(dr["二氧化碳上限"]); }
            catch { }

            try { data.二氧化碳下限 = Convert.ToInt32(dr["二氧化碳下限"]); }
            catch { }

            try { data.土壤盐分上限 = Convert.ToInt32(dr["土壤盐分上限"]); }
            catch { }

            try { data.土壤盐分下限 = Convert.ToInt32(dr["土壤盐分下限"]); }
            catch { }

            return data;
        }
    }
    public class 传感器阈值DataDBOption
    {
        public static int Insert(传感器阈值Data d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into 传感器阈值 (");
            sql.Append("IP, ");
            sql.Append("GreenhouseAddr, ");
            sql.Append("NoteNum, ");
            sql.Append("Time, ");
            sql.Append("空气温度上限, ");
            sql.Append("空气温度下限, ");
            sql.Append("空气湿度上限, ");
            sql.Append("空气湿度下限, ");
            sql.Append("光照上限, ");
            sql.Append("光照下限, ");
            sql.Append("土壤温度上限, ");
            sql.Append("土壤温度下限, ");
            sql.Append("土壤湿度上限, ");
            sql.Append("土壤湿度下限, ");
            sql.Append("二氧化碳上限, ");
            sql.Append("二氧化碳下限, ");
            sql.Append("土壤盐分上限, ");
            sql.Append("土壤盐分下限");
            sql.Append(") values (");
            sql.Append("'");
            sql.Append(d.IP.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.GreenhouseAddr.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.NoteNum.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Time.ToString("yyyy-MM-dd HH:mm:ss"));
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.空气温度上限.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.空气温度下限.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.空气湿度上限.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.空气湿度下限.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.光照上限.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.光照下限.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.土壤温度上限.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.土壤温度下限.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.土壤湿度上限.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.土壤湿度下限.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.二氧化碳上限.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.二氧化碳下限.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.土壤盐分上限.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.土壤盐分下限.ToString());
            sql.Append("");
            sql.Append(")");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static DataTable DataTableSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from 传感器阈值 ");
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static DataTable DataTableSelect(Int32 GreenhouseAddr, Int32 NoteNum)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from 传感器阈值 where (GreenhouseAddr ="+GreenhouseAddr.ToString()+") AND (NoteNum ="+NoteNum.ToString()+")");
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static DataSet DataSetSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from 传感器阈值 ");
            return WFNetLib.ADO.SQLServerOP.DataSetSQL(sql.ToString());
        }
        public static SqlDataReader ReaderSelect(传感器阈值Data d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from 传感器阈值 ");
            return WFNetLib.ADO.SQLServerOP.ReaderExecuteSQL(sql.ToString());
        }
        public static int Update(传感器阈值Data d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from 传感器阈值 ");
            sql.Append("WHERE     (GreenhouseAddr =");
            sql.Append(d.GreenhouseAddr.ToString());
            sql.Append(") AND (NoteNum =");
            sql.Append(d.NoteNum.ToString());
            sql.Append(") AND (IP = '");
            sql.Append(d.IP);
            sql.Append("')");
            DataTable dt = WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
            if (dt.Rows.Count == 0)//库中没有，则为新建
                return Insert(d);
            else//有则为修改
            {
                sql = new StringBuilder();
                sql.Append("UPDATE    传感器阈值 SET ");
                sql.Append("GreenhouseAddr =");
                sql.Append(d.GreenhouseAddr.ToString());
                sql.Append("");
                sql.Append(", ");
                sql.Append("");
                sql.Append("NoteNum =");
                sql.Append(d.NoteNum.ToString());
                sql.Append("");
                sql.Append(", ");
                sql.Append("Time =");
                sql.Append("'");
                sql.Append(d.Time.ToString("yyyy-MM-dd HH:mm:ss"));
                sql.Append("'");
                sql.Append(", ");
                sql.Append("空气温度上限 =");
                sql.Append("");
                sql.Append(d.空气温度上限.ToString());
                sql.Append("");
                sql.Append(", ");
                sql.Append("空气温度下限 =");
                sql.Append("");
                sql.Append(d.空气温度下限.ToString());
                sql.Append("");
                sql.Append(", ");
                sql.Append("空气湿度上限 =");
                sql.Append("");
                sql.Append(d.空气湿度上限.ToString());
                sql.Append("");
                sql.Append(", ");
                sql.Append("空气湿度下限 =");
                sql.Append("");
                sql.Append(d.空气湿度下限.ToString());
                sql.Append("");
                sql.Append(", ");
                sql.Append("光照上限 =");
                sql.Append("");
                sql.Append(d.光照上限.ToString());
                sql.Append("");
                sql.Append(", ");
                sql.Append("光照下限 =");
                sql.Append("");
                sql.Append(d.光照下限.ToString());
                sql.Append("");
                sql.Append(", ");
                sql.Append("土壤温度上限 =");
                sql.Append("");
                sql.Append(d.土壤温度上限.ToString());
                sql.Append("");
                sql.Append(", ");
                sql.Append("土壤温度下限 =");
                sql.Append("");
                sql.Append(d.土壤温度下限.ToString());
                sql.Append("");
                sql.Append(", ");
                sql.Append("土壤湿度上限 =");
                sql.Append("");
                sql.Append(d.土壤湿度上限.ToString());
                sql.Append("");
                sql.Append(", ");
                sql.Append("土壤湿度下限 =");
                sql.Append("");
                sql.Append(d.土壤湿度下限.ToString());
                sql.Append("");
                sql.Append(", ");
                sql.Append("二氧化碳上限 =");
                sql.Append("");
                sql.Append(d.二氧化碳上限.ToString());
                sql.Append("");
                sql.Append(", ");
                sql.Append("二氧化碳下限 =");
                sql.Append("");
                sql.Append(d.二氧化碳下限.ToString());
                sql.Append("");
                sql.Append(", ");
                sql.Append("土壤盐分上限 =");
                sql.Append("");
                sql.Append(d.土壤盐分上限.ToString());
                sql.Append("");
                sql.Append(", ");
                sql.Append("土壤盐分下限 =");
                sql.Append("");
                sql.Append(d.土壤盐分下限.ToString());
                sql.Append("");
                sql.Append("WHERE     (GreenhouseAddr =");
                sql.Append(d.GreenhouseAddr.ToString());
                sql.Append(") AND (NoteNum =");
                sql.Append(d.NoteNum.ToString());
                sql.Append(") AND (IP = '");
                sql.Append(d.IP);
                sql.Append("')");
                return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
            }
        }
        public static int Change大棚信息(string ip, uint oldGreenhouseAddr, byte oldNoteNum, uint newGreenhouseAddr, byte newNoteNum)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("UPDATE    传感器阈值 SET ");
            sql.Append("GreenhouseAddr =");
            sql.Append(newGreenhouseAddr.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append("NoteNum =");
            sql.Append(newNoteNum.ToString());
            sql.Append("");
            sql.Append("WHERE     (GreenhouseAddr =");
            sql.Append(oldGreenhouseAddr.ToString());
            sql.Append(") AND (NoteNum =");
            sql.Append(oldNoteNum.ToString());
            sql.Append(") AND (IP = '");
            sql.Append(ip);
            sql.Append("')");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
    }
}